package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.system.entity.AppButton;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.AppButtonMapper;
import org.jeecg.modules.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.List;

/**
 * @Description: 移动端按钮控制
 * @Author: jeecg-boot
 * @Date:   2019-11-26
 * @Version: V1.0
 */
@Service
public class AppButtonServiceImpl extends ServiceImpl<AppButtonMapper, AppButton> implements IAppButtonService {

    @Autowired
   private AppButtonMapper appButtonMapper;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private ISysUserPositionService sysUserPositionService;


    @Override
    public List<AppButton> queryButtonList(String parentId) {
        return appButtonMapper.queryButtonList(parentId);
    }

    @Override
    public Integer selectBxCount(String userId) {
        //获取该人员部门
        SysDepart departAccept = departService.queryDepartId(userId);
        //该人员对应当前职位
        List<String> positionIds = sysUserPositionService.queryPositionId(userId,departAccept.getParentId());
        Integer repairCount = appButtonMapper.selectAcpectRepairCount();
        Integer taskCount = appButtonMapper.selectRepairTaskCount(userId);
         //管理处接受岗位
        String acceptPosition = appButtonMapper.selectRepairConfig(departAccept.getParentId());
        if (StringUtils.isEmpty(acceptPosition) ){
            return taskCount;
        }
        List<String> configList = Arrays.asList(acceptPosition.split(","));
        //查询指派给该人的task
        boolean flag = false;
       for (String recive:configList) {
            for (String position:positionIds) {
                if (recive.equals(position)){
                    flag = true;
                    taskCount = taskCount +repairCount;
                }
            }
            if (flag){
                break;
            }
        }
        return taskCount;
    }

    @Override
    public List<AppButton> queryAppButtonList() {
        List<AppButton> appButtonTreeModels = appButtonMapper.queryAppButtonList();
        ListIterator<AppButton> AppButtonList = appButtonTreeModels.listIterator();
        while (AppButtonList.hasNext()){
            AppButton appButton = AppButtonList.next();
            if (appButton.getType().equals("menu")){
                List<AppButton> appButtons = appButtonMapper.queryById(appButton.getId());
                appButton.setSunAppButtonList(appButtons);
            }else {
                AppButtonList.remove();
            }
        }

           return appButtonTreeModels;
    }

    @Override
    public Integer selectTxCount(String userId) {
        SysDepart departAccept = departService.queryDepartId(userId);
        List<String> positionIds = sysUserPositionService.queryPositionId(userId,departAccept.getParentId());
        String acceptPosition = appButtonMapper.selectComplainConfig(departAccept.getParentId());
        if (StringUtils.isEmpty(acceptPosition)){
            return 0;
        }
        List<String>  configList= Arrays.asList(acceptPosition.split(","));
        Integer complainCount = appButtonMapper.selectComplainCount();
        for (String recive:configList) {
            for (String position:positionIds) {
                if (recive.equals(position)){
                    return complainCount;
                }
            }
        }
        return null;
    }

    @Override
    public Integer selectXjCount(String userId) {
        return   appButtonMapper.selectInspectionCount(userId);

    }


}
